package com.tanhua.server.utils;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.db.UserStatusInfo;
import com.tanhua.domain.vo.ErrorResult;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UserStatusInfoApi;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

@Component
public class UserStatusUtils {

    @Autowired
    private static UserInfoApi userInfoApi;
    @Autowired
    private static UserStatusInfoApi userStatusInfoApi;

    // 用户状态判断
    public static boolean userStatus(Integer userId,String freezingRange) throws ParseException {
        UserStatusInfo userStatusInfo = userStatusInfoApi.select(userId);
        System.out.println("");
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        String times = sf.format(c.getTime());
        if (userStatusInfo.getUserStatus().equals("2")){
            if (userStatusInfo.getTimeFrame().equals("永久")){
                return true;
            }else if (sf.parse(times).before(sf.parse(userStatusInfo.getTimeFrame())) && userStatusInfo.getFreezingRange().equals(freezingRange)){
                return true;
            }else if (userStatusInfo.getFreezingRange().equals(freezingRange)){
                UserInfo userInfo = new UserInfo();
                userInfo.setId(userId.longValue());
                userInfo.setUserStatus("1");
                userInfoApi.update(userInfo);
                userStatusInfo.setUserStatus("1");
                userStatusInfoApi.update(userStatusInfo);
            }
        }
        return false;
    }
}
